package com.zhuannimei.ui;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.zhuannimei.MainActivity;
import com.zhuannimei.R;
import com.zhuannimei.utility.AudioHelper;
import com.zhuannimei.utility.AudioHelper.AudioSupport;
import com.zhuannimei.utility.WuLog;
import com.zhuannimei.view.OnViewChangeListener;
import com.zhuannimei.view.ZnmScrollLayout;

/**
 * 欢迎引导页
 * 
 * @author Jack 2013-12-11
 * 
 * 
 */
public class WelcomeActivity extends Activity implements OnViewChangeListener,
		OnClickListener {
	
	private static final String TAG = "WelcomeActivity";

	private ZnmScrollLayout main_myScroll;
	private LinearLayout main_point;
	private RelativeLayout main_layout;
	private Button startBtn;

	private int count;
	private ImageView[] imgs;
	private int currentItem;
	private int[] audios  = {R.raw.welcome_01,R.raw.welcome_01,R.raw.welcome_01,R.raw.welcome_01,R.raw.welcome_01,R.raw.welcome_01,R.raw.welcome_01};
	
	private AudioHelper mAudio;
	private MediaPlayer mMp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.welcome);
		initView();
		initEvent();
	}

	private void initEvent() {
		startBtn.setOnClickListener(this);
		main_myScroll.SetOnViewChangeListener(this);
	}

	private void initView() {
		main_myScroll = (ZnmScrollLayout) findViewById(R.id.main_myScroll);
		main_point = (LinearLayout) findViewById(R.id.main_point);
		main_layout = (RelativeLayout) findViewById(R.id.main_layout);
		startBtn = (Button) findViewById(R.id.startBtn);

		count = main_myScroll.getChildCount();
		imgs = new ImageView[count];
		for (int i = 0; i < count; i++) {
			imgs[i] = (ImageView) main_point.getChildAt(i);
			imgs[i].setEnabled(true);
			imgs[i].setTag(i);
		}
		// 设置当前项
		currentItem = 0;
		imgs[currentItem].setEnabled(false);
		
		if(mAudio==null){
			mAudio = new AudioHelper(WelcomeActivity.this);
		}
	}
	/**
	 * "android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.文件名
	 * @param position
	 */
	private void playAudio(int position){
//		AssetFileDescriptor descriptor;
//		descriptor = getResources().openRawResourceFd(audios[position]);
//		try {
//			getResources().getAssets().open("welcome_01.amr");
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//		WuLog.i(TAG, ""+descriptor.getFileDescriptor());
//		String aPath = "android.resource://" + getApplicationContext().getPackageName() + "/" + audios[position];
//		WuLog.i(TAG, ""+aPath);
//		mAudio.startPlay(descriptor.getFileDescriptor(), new AudioSupport() {
//			@Override
//			public void onStop() {
//				
//			}
//		});
		stopAudio();
		mMp = MediaPlayer.create(WelcomeActivity.this, audios[position]);
		mMp.start();
	}
	/**
	 * 
	 */
	private void stopAudio(){
//		if(mAudio.isPlaying()){
//			mAudio.stopPlayer(true);
//		}
		if(mMp!=null && mMp.isPlaying()){
			mMp.stop();
		}
		
	}

	@Override
	public void OnViewChange(int position) {
		WuLog.i(TAG, "页码切换"+position);
		setCurrentPoint(position);
		playAudio(position);
	}

	@Override
	public void onClick(View v) {
		WuLog.i(TAG, "点击开启");
		stopAudio();
		Intent intent = new Intent(this, MainActivity.class);
		startActivity(intent);
		this.finish();
	}
	
	private void setCurrentPoint(int position){
		if(position < 0 || position >count-1 || currentItem == position)
			return;
		imgs[currentItem].setEnabled(true);
		imgs[position].setEnabled(false);
		currentItem = position;
	}

}
